package top.zsyle.org.controller;

import com.sun.deploy.net.HttpResponse;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.zsyle.basic.util.AjaxResult;
import top.zsyle.basic.util.ExcelUtil;
import top.zsyle.basic.util.PageList;
import top.zsyle.org.domain.Shop;
import top.zsyle.org.dto.ShopAuditDto;
import top.zsyle.org.dto.ShopSettlementAgainDto;
import top.zsyle.org.query.ShopQuery;
import top.zsyle.org.service.IShopService;
import top.zsyle.org.dto.ShopSettlementDto;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.websocket.server.PathParam;
import java.util.List;
import java.util.Map;

@RestController()
@RequestMapping("/shop")
@Api(value = "门店的API",description="门店相关的CRUD功能")
public class ShopController {

    @Autowired
    private IShopService shopService;


    /**
     * 查询全部
     * @return
     */
    @GetMapping()
    public List<Shop> findAll(){
        return shopService.findAll();
    }


    @GetMapping("/{id}")
    public Shop getById(@PathVariable("id") Long id){

        return shopService.findById(id);
    }

    /**
     * 店铺入驻请求接口
     * @param dto
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody @Valid ShopSettlementDto dto){
        shopService.settlement(dto);
        return AjaxResult.me().success();
    }

    /**
     * 再次店铺入驻
     * @param dto
     * @return
     */
    @PostMapping("/settlement/again")
    public AjaxResult settlementAgain(@RequestBody @Valid ShopSettlementAgainDto dto){
        shopService.settlementAgain(dto);
        return AjaxResult.me().success();
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @PostMapping("/list")
    public PageList<Shop> queryPage(@RequestBody ShopQuery query){
        return shopService.queryByPage(query);
    }



    /**
     * 审核
     * @param dto
     * @return
     */
    @PostMapping("/audit")
    public AjaxResult audit(@RequestBody ShopAuditDto dto){
        shopService.audit(dto);
        return AjaxResult.me().success();
    }

    /**
     * 激活接口
     * @param id
     * @return
     */
    @GetMapping("/active/{id}")
    public AjaxResult active(@PathVariable("id") Long id){
        shopService.active(id);
        return AjaxResult.me().success();
    }

    /**
     * 导出接口
     * @param response
     */
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response){
        List<Shop> all = shopService.findAll();
        ExcelUtil.exportExcel(all,null,"店铺信息",Shop.class,"店铺信息.xls",response);
    }

    /**
     * 导入excel文件
     * @param file
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestParam("file") MultipartFile file){
        List<Shop> all = ExcelUtil.importExcel(file,0,1,Shop.class);
        all.forEach(System.out::println);
    }

    /**
     * 获取echarts接口
     * @return
     */
    @GetMapping("/echarts")
    public Map<String,Object> loadShopEcharts(){
         return shopService.loadShopEcharts();
    }

}
